package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.CinemaBackMapper;
import com.zretc.dao.MovieFirstPageMapper;
import com.zretc.entity.Movie;
import com.zretc.service.MovieFirstPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author qiaozhiyu
 * @date 2020-7-21
 */
@Service
public class MovieFirstPageServiceImpl implements MovieFirstPageService {

    @Autowired
    private MovieFirstPageMapper movieFirstPageMapper;

    @Autowired
    private CinemaBackMapper cinemaBackMapper;

    @Override
    public PageInfo<Movie> getMovieByStatus(Map<String, String> params) {

        // 获取当前页数
        int pageNum = Integer.parseInt(params.get("pageNum"));
        // 获取条数
        int pageSize = Integer.parseInt(params.get("pageSize"));
        // 获取状态
        int showStatus = Integer.parseInt(params.get("showStatus"));


        // 1.开始分页
        PageHelper.startPage(pageNum,pageSize);

        // 调用dao层实现
        List<Movie> movieByStatus = movieFirstPageMapper.findMovieByStatus(showStatus);

        // 3.包装到PageInfo对象
        PageInfo<Movie> PageInfo = new PageInfo<>(movieByStatus);

        return PageInfo;
    }

    @Override
    public List<Map<String, Object>> getMovieByStartTime(String startTime) {

        Map<String, Object> map = null;
        List<String> actorList = null;
        List<Map<String, Object>> movieByStartTime = null;

        movieByStartTime = movieFirstPageMapper.findMovieByStartTime(startTime);
        for(int i = 0; i < movieByStartTime.size(); i++){
            map = movieByStartTime.get(i);
            String movieName = map.get("movieName").toString();
            actorList = movieFirstPageMapper.findActorByMovieId(movieName);

            // 将集合放入map中
            map.put("actorList",actorList);

        }

        // 放到大的集合里
        movieByStartTime.add(map);

        return movieByStartTime;
    }

    @Override
    public PageInfo<Movie> getMovieById(Map<String, String> params) {

        // 获取当前页数
        int pageNum = Integer.parseInt(params.get("pageNum"));
        // 获取条数
        int pageSize = Integer.parseInt(params.get("pageSize"));
        // 获取影院名
        String cinemaName = params.get("cinemaName");

        // Map<String,List<Movie>> movieMap = null;
        // PageInfo<Movie> PageInfo = null;
        List<Map<String, String>> movieIdByCinemaName = cinemaBackMapper.findMovieIdByCinemaName(cinemaName);

        String cinemaId = movieIdByCinemaName.get(0).get("cinemaId");

        // 1.开始分页
        PageHelper.startPage(pageNum,pageSize);

        List<Movie> movieList = movieFirstPageMapper.findMovieByRelationCinemaId(cinemaId);

        // 3.包装到PageInfo对象
        PageInfo<Movie> PageInfo = new PageInfo<>(movieList);

        return PageInfo;
    }

    @Override
    public boolean updateMovieShowStatus(String movieName) {

        int showStatus = movieFirstPageMapper.updateShowStatus(movieName);
        if(showStatus != 0){
            return true;
        } else{
            return false;
        }

    }
}
